package com.engine.jucailinkq.attendance.workflow.action.askforleave;

import com.engine.jucailinkq.common.util.CommonUtil;
import com.engine.jucailinkq.common.util.DateUtil;
import com.engine.jucailinkq.common.util.DbTools;
import com.engine.jucailinkq.common.util.Utils;
import com.google.common.collect.Maps;
import lombok.extern.slf4j.Slf4j;
import weaver.general.Util;
import weaver.interfaces.workflow.action.Action;
import weaver.soa.workflow.request.RequestInfo;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @Author: sy
 * @Description: 批量请假-退回到提交节点附加操作
 * @Date: 2024/8/16
 **/
@Slf4j
public class BatchAskForLeaveWorkFlowBackAction implements Action {
    @Override
    public String execute(RequestInfo requestInfo) {
        String requestid = requestInfo.getRequestid();
        int billid = requestInfo.getRequestManager().getBillid();
        log.info("billid : [{}]",billid);

        String sql = "select id, jlzt from uf_jcl_kq_qjjl where djid = " + requestid;
        Map<String, Object> qjllData = DbTools.getSqlToMap(sql);
        String jlzt = Util.null2String(qjllData.get("jlzt"));

        // 流程表单主表数据
        HashMap<String,String> mainTableData = CommonUtil.getMainTableInfo(requestInfo);
        log.info("mainTableData : [{}]",mainTableData);
        // 流程表单明细表1数据
        List<Map<String, String>> detailTableData = CommonUtil.getDetailTableInfo(requestInfo,0);
        log.info("detailTableData : [{}]",detailTableData);

        String detail1TableName = requestInfo.getDetailTableInfo().getDetailTable()[0].getTableDBName();
        log.info("detail1ableName : [{}]", detail1TableName);

        //流程表单明细表2数据
        List<Map<String, String>> detailTable2 = CommonUtil.getDetailTableInfo(requestInfo,1);
        log.info("BatchAskForLeaveWorkFlowBackAction-detailTable2Data : [{}]",detailTable2);

        String detail2TableName = requestInfo.getDetailTableInfo().getDetailTable()[1].getTableDBName();
        log.info("detail2ableName : [{}]", detail2TableName);

        sql = "select id,jqid,sxrq,ktsc,yxsc,wxsc,yqsxrq,ztsc from uf_jcl_kq_jqye order by sxrq";

        List<Map<String,Object>> holidayBalanceList = DbTools.getSqlToList(sql);

        Map<String,Object> holidayBalanceMap = holidayBalanceList.stream().collect(Collectors.toMap(e->e.get("id").toString(), e->e));

        try {
            if (detailTable2.size() > 0) {
                Map<String,Object> balanceMap = Maps.newHashMap();
                for (Map<String, String> data : detailTable2){
                    String jqye = Util.null2String(data.get("jqye"));
                    double sysc = Double.valueOf(data.get("sysc").toString());
                    double havedSysc= balanceMap.get(jqye) ==null?0:Double.valueOf(balanceMap.get(jqye).toString());
                    sysc = Utils.add(sysc,havedSysc);
                    balanceMap.put(jqye,sysc);
                }
                log.info("balanceMap : [{}]",balanceMap);
                for (Map.Entry<String,Object> entry:balanceMap.entrySet()){
                    String id = entry.getKey();
                    double ztsc = Double.valueOf(entry.getValue().toString());
                    Map<String,Object> map =  (Map<String,Object>)holidayBalanceMap.get(id);
                    boolean updateSign = true;
                    log.info("jlzt : [{}]",jlzt);
                    if ("1".equals(jlzt)) {
                        double havedyxsc = Util.null2String(map.get("yxsc")).equals("") ? 0 : Double.valueOf(map.get("yxsc").toString());
                        double havedwxsc = Util.null2String(map.get("wxsc")).equals("") ? 0 : Double.valueOf(map.get("wxsc").toString());
                        double updatedyxsc = Utils.subtract(havedyxsc,ztsc);
                        double updatedwxsc = Utils.add(havedwxsc,ztsc);

                        log.info("updatedyxsc : [{}],id:[{}]",updatedyxsc,id);
                        String updateSql = "update uf_jcl_kq_jqye set wxsc=?,yxsc=? where id=?";
                        updateSign = DbTools.update(updateSql,updatedwxsc, updatedyxsc,id);
                    } else if("0".equals(jlzt)){
                        double havedztsc = Util.null2String(map.get("ztsc")).equals("") ? 0 : Double.valueOf(map.get("ztsc").toString());
                        double havedwxsc = Util.null2String(map.get("wxsc")).equals("") ? 0 : Double.valueOf(map.get("wxsc").toString());

                        double updatedztsc = Utils.subtract(havedztsc,ztsc);
                        double updatedwxsc = Utils.add(havedwxsc,ztsc);

                        log.info("updatedwxsc : [{}],updatedztsc:[{}],id:[{}]",updatedwxsc,updatedztsc,id);
                        String updateSql = "update uf_jcl_kq_jqye set wxsc=?,ztsc=? where id=?";
                        updateSign = DbTools.update(updateSql,updatedwxsc,updatedztsc,id);
                    }

                    log.info("updateSign : [{}]",updateSign);
                    if (!updateSign){
                        //更新假期余额失败
                        String message = "更新假期余额失败";
                        log.error(message);
                        requestInfo.getRequestManager().setMessageid("11111" + requestInfo.getRequestid() + "22222");
                        requestInfo.getRequestManager().setMessagecontent(message);
                        return Action.FAILURE_AND_CONTINUE;
                    }
                }
            }

            //删除请假记录
            if ("1".equals(jlzt) || "0".equals(jlzt)) {
                String qjjlMainId = Util.null2String(qjllData.get("id"));
                if (!"".equals(qjjlMainId)) {
                    String delteSql = "delete from uf_jcl_kq_qjjl_dt1 where mainid = " + qjjlMainId;
                    boolean delSignDt1 = DbTools.update(delteSql);
                    delteSql = "delete from uf_jcl_kq_qjjl_dt2 where mainid = " + qjjlMainId;
                    boolean delSignDt2 = DbTools.update(delteSql);
                    delteSql = "delete from uf_jcl_kq_qjjl where id = " + qjjlMainId;
                    boolean delSignMain = DbTools.update(delteSql);
                    if (!delSignDt1 || !delSignDt2 || !delSignMain) {
                        String message = "删除假期记录失败";
                        log.error(message);
                        requestInfo.getRequestManager().setMessageid("11111" + requestInfo.getRequestid() + "22222");
                        requestInfo.getRequestManager().setMessagecontent(message);
                        return Action.FAILURE_AND_CONTINUE;
                    }
                }
            }
            //将明细表1数据清空
            String delSql = "delete from " + detail1TableName + " where mainid=?";
            if (!DbTools.update(delSql, billid)) {
                //将明细表1数据清空
                String message = "删除流程明细表1数据失败";
                log.error(message);
                requestInfo.getRequestManager().setMessageid("11111" + requestid + "22222");
                requestInfo.getRequestManager().setMessagecontent(message);
                return Action.FAILURE_AND_CONTINUE;
            }

            //将明细表2数据清空
            delSql = "delete from " + detail2TableName + " where mainid=?";
            if (!DbTools.update(delSql, billid)) {
                //将明细表2数据清空
                String message = "删除流程明细表2数据失败";
                log.error(message);
                requestInfo.getRequestManager().setMessageid("11111" + requestid + "22222");
                requestInfo.getRequestManager().setMessagecontent(message);
                return Action.FAILURE_AND_CONTINUE;
            }


        }catch (Exception e){
            log.error("BatchAskForLeaveWorkFlowBackAction error : [{}]",e);
            return Action.FAILURE_AND_CONTINUE;
        }


        return Action.SUCCESS;
    }
}
